TASK_DIR:=$(dir $(abspath $(lastword $(MAKEFILE_LIST))))

include ../config.mk

# Inputs
SRC := $(TASK_DIR)/src/prepare_data_and_covs_for_regressions.R
COVARIATE_SETS_CONFIG_FILE := $(TASK_DIR)/../run_regressions/hand/covariate_sets.yaml
DL_REG_SCRIPT := $(TASK_DIR)/src/run_stata_double_lasso_regressions.do
RI_REG_SCRIPT := $(TASK_DIR)/src/run_stata_RI_regressions.do

# Outputs
PARAM_FILE := $(OUT_DIR)/load_parameters.do
XSECTION_ANALYSIS_FILE_CSV := $(OUT_DIR)/readi_outcomes_roster_up_to_date.csv
DL_OUT_DIR := $(OUT_DIR)/double_lasso/
RI_OUT_DIR := $(OUT_DIR)/randomization_inference/
DL_OUT_FILE := $(DL_OUT_DIR)/i_three_components_post_20.xlsx
RI_OUT_FILE := $(RI_OUT_DIR)/regressions_xsection_RI.csv
RI_OUT_FILE_BY_PATHWAY := $(RI_OUT_DIR)/regressions_xsection_by_pathway_RI.csv


$(PARAM_FILE): $(SRC) $(PROJECT_CONFIG) $(COVARIATE_SETS_CONFIG_FILE) $(XSECTION_ANALYSIS_FILE)
	@mkdir -p $(OUT_DIR)
	@$(R_script) $(SRC) \
		--PROJECT_CONFIG=$(PROJECT_CONFIG) \
		--COVARIATE_SETS_CONFIG_FILE=$(COVARIATE_SETS_CONFIG_FILE) \
		--XSECTION_ANALYSIS_FILE=$(XSECTION_ANALYSIS_FILE) \
		--XSECTION_ANALYSIS_FILE_CSV=$(XSECTION_ANALYSIS_FILE_CSV) \
		--PARAM_FILE=$(PARAM_FILE)

$(DL_OUT_FILE): $(DL_REG_SCRIPT) $(PARAM_FILE) $(XSECTION_ANALYSIS_FILE_CSV)
	@mkdir -p $(DL_OUT_DIR)
	stata -b do $(DL_REG_SCRIPT) $(PARAM_FILE) $(XSECTION_ANALYSIS_FILE_CSV) $(DL_OUT_FILE) \
		$(DL_OUT_DIR)a_shooting_or_homicide_post_20.xlsx \
		$(DL_OUT_DIR)a_partone_non_shooting_or_homicide_post_20.xlsx \
		$(DL_OUT_DIR)v_shooting_or_homicide_post_20.xlsx

$(RI_OUT_FILE_BY_PATHWAY): $(RI_REG_SCRIPT) $(PARAM_FILE) $(XSECTION_ANALYSIS_FILE_CSV)
	@mkdir -p $(RI_OUT_DIR)
	stata -b do $(RI_REG_SCRIPT) $(PARAM_FILE) $(XSECTION_ANALYSIS_FILE_CSV) $(RI_OUT_DIR) $(RI_OUT_FILE) $(RI_OUT_FILE_BY_PATHWAY)


.PHONY: all clean
.DEFAULT_GOAL=all

all: $(DL_OUT_FILE) $(RI_OUT_FILE_BY_PATHWAY)

clean:
	rm -rf $(OUT_DIR)


